/* file: realnode.h */

#ifndef _REAL_NODE_H
#define _REAL_NODE_H

#include "gp.h"
#include "cg_node.h"
#include "cg_image.h"
#include "correspond.h"

typedef struct
{
    int level;
    void *ptr;
    char send_ip[20];
    char filename[30];
} Arg;

typedef struct
{
    void *REAL_ptr;
    void *CG_ptr;
} InitArg;

typedef struct
{
    int connfd;
    char ip[20];
    void *REAL_ptr;
} fileArg;

typedef struct
{
    void *REAL_ptr;
    char ip_file[100];
} servArg;

/*************************************************
class name: RealNode	
*************************************************/
class RealNode
{
protected:
    string ip;
    CGImage cgtree;
    map<int, CGNode> nodes;
    CGNodeStatus status;
    char str[BUFF_MAXSIZE];
    Correspond recv_st, recv_hb, recv_rs;

    int file_listenfd;
    int load;
    map<string, int> link;
    map<string,int> task;

    
    int ready;
    int level;

public:
    char ip_file[BUFF_MAXSIZE];

public:
    void initRealNode();
    void run();

public:
    void* recvMessage(void *arg);
    void* recvMesThread(void *arg);
    void* recvFile(void *arg);
    void* recvFileThread(int connfd, char *ip);
    void* recvHeartBeat(void *arg);
    void* heartBeat(void *arg);

    int getDepth() { return cgtree.getDepth(); }
    string getRoot() { return cgtree.getRoot(); }
    
    void *findServer();
    void rootServeCli(char *str);
    
    void do_service(char *str);
    int sendFile(string ip, char *file);
    int receiveFile(char * filename);

    int listenFile();
    int getLoad() { return load; }
    void addLoad() { load++; }
    void deleteLoad() { load--; }
};


#endif
